<?php
class Product extends AppModel {
	var $name = 'Product';
	//The Associations below have been created with all possible keys, those that are not needed can be removed
/*
	var $hasAndBelongsToMany = array(
		'Client' => array(
			'className' => 'Client',
			'joinTable' => 'clients_products',
			'foreignKey' => 'product_id',
			'associationForeignKey' => 'client_id',
			'unique' => true,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'finderQuery' => '',
			'deleteQuery' => '',
			'insertQuery' => ''
		),
		'User' => array(
			'className' => 'User',
			'joinTable' => 'products_users',
			'foreignKey' => 'product_id',
			'associationForeignKey' => 'user_id',
			'unique' => true,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'finderQuery' => '',
			'deleteQuery' => '',
			'insertQuery' => ''
		)
	);
*/
	
	function ObtieneProductos($sucursal_id=0, $user_id=0, $specialty_id=0){
		//-------------------------------------------------------------------
		//                       Obtiene los productos asociados a esa agenda
		//-------------------------------------------------------------------
		if( $user_id>0 ){

	        // --- Obtiene los productos de ese medico
	        $sql = "select products.id, products.name from products 
	                left join products_users on products_users.product_id = products.id
	                where products_users.user_id='$user_id' ";

		} else {

	        $sql = "select products.id, products.name
					from products_users as pu
						left join sucursals_users as su on su.user_id = pu.user_id
						left join products on products.id = pu.product_id
						left join specialties_users as spu on su.user_id = spu.user_id
					where su.sucursal_id = '$sucursal_id' and spu.specialty_id = '$specialty_id'
					group by pu.product_id";
		}

		$x = $this->query($sql);
	    $Productos = array();
	    foreach($x as $xp){
	    	$Productos[$xp['products']['id']] = $xp['products']['name'];
	    }
	    return $Productos;




	}

}
?>